package com.fengye.rabbit.order.controller;

import com.fengye.rabbit.order.dto.UserOrderDTO;
import com.fengye.rabbit.order.response.BaseResponse;
import com.fengye.rabbit.order.response.StatusCode;
import com.fengye.rabbit.order.service.UserOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author sky
 * @date 2021年06月10日 1:38 下午
 */
@RestController
@Slf4j
public class UserOrderController {

    @Autowired
    UserOrderService userOrderService;

    @PostMapping(value = "/user/order", consumes = MediaType.APPLICATION_JSON_VALUE)
    public BaseResponse order(@RequestBody @Validated UserOrderDTO dto, BindingResult result) {
        if (result.hasErrors()) {
            return new BaseResponse(StatusCode.InvalidParams);
        }

        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            userOrderService.pushUserOrder(dto);
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(), e.getMessage());
        }
        return response;
    }
}
